package dao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;

import bean.CombinaisonBean;

	public class DaoCombinaison extends Dao {

	Properties sql = new Properties();
	public DaoCombinaison() {			
		try {
			FileInputStream in = new FileInputStream(config.getProperty("sqlCombinaison"));
			sql.load(in);
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	

	private CombinaisonBean rsToBean(ResultSet rs){
		CombinaisonBean cb = new CombinaisonBean();
		try {
			while (rs.next()) {
				cb.setIdCombinaison(rs.getInt("idcombinaison"));
				cb.setP1(rs.getInt("p1"));
				cb.setP2(rs.getInt("p2"));
				cb.setP3(rs.getInt("p3"));
				cb.setP4(rs.getInt("p4"));
				cb.setName(rs.getString("name"));
				cb.setIdCoder(rs.getInt("idcoder"));
				}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cb;
	}
	private ArrayList<CombinaisonBean> rsToArrayList(ResultSet rs){
		ArrayList<CombinaisonBean> combinaisonList = new ArrayList<CombinaisonBean>();
		try {
			while (rs.next()) {
				CombinaisonBean cb = new CombinaisonBean(); 
				cb.setIdCombinaison(rs.getInt("idcombinaison"));
				cb.setP1(rs.getInt("p1"));
				cb.setP2(rs.getInt("p2"));
				cb.setP3(rs.getInt("p3"));
				cb.setP4(rs.getInt("p4"));
				cb.setName(rs.getString("name"));
				cb.setIdCoder(rs.getInt("idcoder"));
				combinaisonList.add(cb);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return combinaisonList;
		
	}
	public CombinaisonBean getCombinaisonByName(String name){
		
		try {
			PreparedStatement pst= con.prepareStatement(sql.getProperty("getCombinaisonByName"));
			pst.setString(1, name);
			ResultSet rs = pst.executeQuery();
			return rsToBean(rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	public CombinaisonBean getCombinaisonById(int idCombinaison){
				try {
					PreparedStatement pst = con.prepareStatement(sql.getProperty("getCombinaisonById"));
					pst.setInt(1, idCombinaison);
					ResultSet rs = pst.executeQuery();
					return rsToBean(rs);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}		
	}
	
	public CombinaisonBean create(CombinaisonBean cb){
			try {
				PreparedStatement pst = con.prepareStatement(sql.getProperty("Create"));
				pst.setString(1, cb.getName());
				pst.setInt(2,cb.getP1());
				pst.setInt(3,cb.getP2());
				pst.setInt(4,cb.getP3());
				pst.setInt(5,cb.getP4());
				pst.setInt(6, cb.getIdCoder());
				pst.executeUpdate();
				pst.close();
				return getCombinaisonByName(cb.getName());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
	}
	public ArrayList<CombinaisonBean> getNotFindCombinaisons(int idUser){
		try {
			PreparedStatement pst = con.prepareStatement(sql.getProperty("getNotFindCombinaisons"));
			pst.setInt(1,idUser);
			return rsToArrayList(pst.executeQuery());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	public ArrayList<CombinaisonBean> getOwnedCombinaisons(int idUser){
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql.getProperty("getOwnedCombinaisons"));
			pst.setInt(1,idUser);
			return rsToArrayList(pst.executeQuery());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
	public int update(CombinaisonBean cb){
		try {
			Statement stmt = con.createStatement();
		
			String sql ="UPDATE Combinaisons SET (name,p1,p2,p3,p4) values ("+cb.getName()+"," + cb.getP1() +

					 	"," + cb.getP2()+ "," + cb.getP3() + "," + cb.getP4() +") where idcombinaison="+cb.getIdCombinaison()+";";
			return stmt.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	public ArrayList<CombinaisonBean> getAllCombinaisons(){
		try {
			PreparedStatement pst = con.prepareStatement(sql.getProperty("getAllCombinaisons"));
			return rsToArrayList(pst.executeQuery());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
	public ArrayList<CombinaisonBean> getFindCombinaisons(int idUser){
		try {
			PreparedStatement pst = con.prepareStatement(sql.getProperty("getFindCombinaisons"));
			pst.setInt(1,idUser);
			return rsToArrayList(pst.executeQuery());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean checkExistsName (CombinaisonBean cb){
		try {
			
			PreparedStatement pst = con.prepareStatement(sql.getProperty("checkExistsName"));
			pst.setString(1,cb.getName());
			ResultSet rs = pst.executeQuery();
			//pst.close();
			if(rs.next()){
				return true;
			}else{
				return false;
			}
				
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			return true;
		}
	}
	public boolean checkExistsCombinaison(CombinaisonBean cb){
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql.getProperty("checkExistsCombinaison"));
			pst.setInt(1,cb.getP1());
			pst.setInt(2,cb.getP2());
			pst.setInt(3,cb.getP3());
			pst.setInt(4,cb.getP4());
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				return true;
			}else{
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return true;	
		}
	}
}
